package com.huateng.springcloud.thread;

import java.lang.management.ManagementFactory;
import java.math.BigDecimal;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author Evan
 * @create 2021/7/27 15:39
 */
public class MyPoolSizeCal extends PoolSizeCalculator {

    public static void main(String[] args) throws Exception {
        MyPoolSizeCal cal = new MyPoolSizeCal();
        cal.calculateBoundaries(
                //CPU目标利用率
                BigDecimal.ONE,
                //blockingqueue占用的内存大小，byte
                new BigDecimal(100000)
        );
    }

    @Override
    protected Runnable creatTask() {
        return new AsynchronousTask();
    }

    @Override
    protected BlockingQueue<Runnable> createWorkQueue() {
        return new LinkedBlockingQueue<>();
    }

    @Override
    protected long getCurrentThreadCPUTime() {
        //当前线程占用的总时间
        return ManagementFactory.getThreadMXBean().getCurrentThreadCpuTime();
    }
}

class AsynchronousTask implements Runnable {

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName());
    }

}
